var t = require("../../utils/utils"), a = getApp().globalData, o = a.key, e = a.systeminfo;

Page({
    data: {
        isIPhoneX: a.isIPhoneX,
        message: "",
        cityDatas: {},
        hourlyDatas: [],
        sharelist: [ {
            url: "/img/share02.jpg",
            name: "从遇见你的那一刻，你就让我分心"
        }, {
            url: "/img/share02.jpg",
            name: "愿你有诗有酒有良人"
        }, {
            url: "/img/share03.jpg",
            name: "除了喜欢你，我什么都做不好"
        }, {
            url: "/img/share04.jpg",
            name: "见到你，我觉得多少适应了这个世界"
        }, {
            url: "/img/share05.jpg",
            name: "愿有岁月可回首，且以深情共白头"
        }, {
            url: "/img/share06.jpg",
            name: "想和你喝酒是假的，想醉你怀里是真的"
        }, {
            url: "/img/share07.jpg",
            name: "海底月是天上月，眼前人是心上人"
        }, {
            url: "./img/share08.jpg",
            name: "你陪着我的时候，我从没羡慕过任何人"
        } ],
        weatherIconUrl: a.weatherIconUrl,
        detailsDic: {
            key: [ "tmp", "fl", "hum", "pcpn", "wind_dir", "wind_deg", "wind_sc", "wind_spd", "vis", "pres", "cloud", "" ],
            val: {
                tmp: "温度(℃)",
                fl: "体感温度(℃)",
                hum: "相对湿度(%)",
                pcpn: "降水量(mm)",
                wind_dir: "风向",
                wind_deg: "风向角度(deg)",
                wind_sc: "风力(级)",
                wind_spd: "风速(mk/h)",
                vis: "能见度(km)",
                pres: "气压(mb)",
                cloud: "云量"
            }
        },
        lifestyles: {
            comf: "舒适度指数",
            cw: "洗车指数",
            drsg: "穿衣指数",
            flu: "感冒指数",
            sport: "运动指数",
            trav: "旅游指数",
            uv: "紫外线指数",
            air: "空气污染扩散条件指数",
            ac: "空调开启指数",
            ag: "过敏指数",
            gl: "太阳镜指数",
            mu: "化妆指数",
            airc: "晾晒指数",
            ptfc: "交通指数",
            fsh: "钓鱼指数",
            spi: "防晒指数"
        },
        searchText: "",
        hasPopped: !1,
        animationMain: {},
        animationOne: {},
        animationTwo: {},
        located: !0,
        searchCity: "",
        setting: {},
        bcgImgList: [ {
            src: "/img/qing.jpg",
            topColor: "#0085e5"
        }, {
            src: "/img/yu.jpg",
            topColor: "#0e202c"
        }, {
            src: "/img/xue.jpg",
            topColor: "#0f0e1c"
        }, {
            src: "/img/yun.jpg",
            topColor: "#004092"
        }, {
            src: "/img/wu.jpg",
            topColor: "#d3ebf5"
        }, {
            src: "/img/yin.jpg",
            topColor: "#2d2225"
        }, {
            src: "/img/bg5.jpg",
            topColor: "#b8bab9"
        } ],
        bcgImgIndex: 0,
        bcgImg: "",
        bcgImgAreaShow: !1,
        bcgColor: "#2d2225",
        showHeartbeat: !0,
        enableSearch: !0,
        openSettingButtonShow: !1,
        shareInfo: {}
    },
    success: function(a, o) {
        this.setData({
            openSettingButtonShow: !1,
            searchCity: o
        }), wx.stopPullDownRefresh();
        var e = new Date();
        a.updateTime = e.getTime(), a.updateTimeFormat = t.formatDate(e, "MM-dd hh:mm"), 
        wx.setStorage({
            key: "cityDatas",
            data: a
        }), this.setData({
            cityDatas: a
        });
    },
    fail: function(t) {
        var a = this;
        wx.stopPullDownRefresh();
        var o = t.errMsg || "";
        -1 !== o.indexOf("deny") || -1 !== o.indexOf("denied") ? wx.showToast({
            title: "需要开启地理位置权限",
            icon: "none",
            duration: 2500,
            success: function(t) {
                if (a.canUseOpenSettingApi()) var o = setTimeout(function() {
                    clearTimeout(o), wx.openSetting({});
                }, 2500); else a.setData({
                    openSettingButtonShow: !0
                });
            }
        }) : wx.showToast({
            title: "网络不给力，请稍后再试",
            icon: "none"
        });
    },
    commitSearch: function(t) {
        var a = ((t.detail || {}).value || "").replace(/\s+/g, "");
        this.search(a);
    },
    dance: function() {
        var t = this;
        this.setData({
            enableSearch: !1
        }), this.selectComponent("#heartbeat").dance(function() {
            t.setData({
                showHeartbeat: !1,
                enableSearch: !0
            }), t.setData({
                showHeartbeat: !0
            });
        });
    },
    clearInput: function() {
        this.setData({
            searchText: ""
        });
    },
    search: function(t, a) {
        if ("林妹妹" === t || "林小猪" === t || "林秋兰" === t || "狐狸精" === t) return this.clearInput(), 
        void this.dance();
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), t && (this.setData({
            located: !1
        }), this.getWeather(t), this.getHourly(t)), a && a();
    },
    canUseOpenSettingApi: function() {
        var a = getApp().globalData.systeminfo.SDKVersion;
        return t.cmpVersion(a, "2.0.7") < 0;
    },
    init: function(t, a) {
        var o = this;
        this.setData({
            located: !0
        }), wx.getLocation({
            success: function(t) {
                o.getWeather(t.latitude + "," + t.longitude), o.getHourly(t.latitude + "," + t.longitude), 
                a && a();
            },
            fail: function(t) {
                o.fail(t);
            }
        });
    },
    getWeather: function(t) {
        var e = this;
        wx.request({
            url: "" + a.requestUrl.weather,
            data: {
                location: t,
                key: o
            },
            success: function(a) {
                if (200 === a.statusCode) {
                    var o = a.data.HeWeather6[0];
                    "ok" === o.status ? (e.clearInput(), e.success(o, t), o.now.cond_txt.indexOf("晴") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[0].src,
                        bcgColor: e.data.bcgImgList[0].topColor
                    }), e.setNavigationBarColor()) : o.now.cond_txt.indexOf("雨") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[1].src,
                        bcgColor: e.data.bcgImgList[1].topColor
                    }), e.setNavigationBarColor()) : o.now.cond_txt.indexOf("雪") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[2].src,
                        bcgColor: e.data.bcgImgList[2].topColor
                    }), e.setNavigationBarColor()) : o.now.cond_txt.indexOf("云") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[3].src,
                        bcgColor: e.data.bcgImgList[3].topColor
                    }), e.setNavigationBarColor()) : o.now.cond_txt.indexOf("雾") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[4].src,
                        bcgColor: e.data.bcgImgList[4].topColor
                    }), e.setNavigationBarColor()) : o.now.cond_txt.indexOf("阴") >= 0 ? (e.setData({
                        bcgImg: e.data.bcgImgList[5].src,
                        bcgColor: e.data.bcgImgList[5].topColor
                    }), e.setNavigationBarColor()) : (e.setData({
                        bcgImg: e.data.bcgImgList[6].src,
                        bcgColor: e.data.bcgImgList[6].topColor
                    }), e.setNavigationBarColor())) : wx.showToast({
                        title: "查询失败",
                        icon: "none"
                    });
                }
            },
            fail: function() {
                wx.showToast({
                    title: "查询失败",
                    icon: "none"
                });
            }
        });
    },
    getHourly: function(t) {
        var e = this;
        wx.request({
            url: "" + a.requestUrl.hourly,
            data: {
                location: t,
                key: o
            },
            success: function(t) {
                if (200 === t.statusCode) {
                    var a = t.data.HeWeather6[0];
                    "ok" === a.status && e.setData({
                        hourlyDatas: a.hourly || []
                    });
                }
            },
            fail: function() {
                wx.showToast({
                    title: "查询失败",
                    icon: "none"
                });
            }
        });
    },
    onPullDownRefresh: function(t) {
        this.reloadPage();
    },
    getCityDatas: function() {
        var t = this;
        wx.getStorage({
            key: "cityDatas",
            success: function(a) {
                t.setData({
                    cityDatas: a.data
                }), a.data.now.cond_txt.indexOf("晴") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[0].src,
                    bcgColor: t.data.bcgImgList[0].topColor
                }), t.setNavigationBarColor()) : a.data.now.cond_txt.indexOf("雨") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[1].src,
                    bcgColor: t.data.bcgImgList[1].topColor
                }), t.setNavigationBarColor()) : a.data.now.cond_txt.indexOf("雪") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[2].src,
                    bcgColor: t.data.bcgImgList[2].topColor
                }), t.setNavigationBarColor()) : a.data.now.cond_txt.indexOf("云") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[3].src,
                    bcgColor: t.data.bcgImgList[3].topColor
                }), t.setNavigationBarColor()) : a.data.now.cond_txt.indexOf("雾") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[4].src,
                    bcgColor: t.data.bcgImgList[4].topColor
                }), t.setNavigationBarColor()) : a.data.now.cond_txt.indexOf("阴") >= 0 ? (t.setData({
                    bcgImg: t.data.bcgImgList[5].src,
                    bcgColor: t.data.bcgImgList[5].topColor
                }), t.setNavigationBarColor()) : (t.setData({
                    bcgImg: t.data.bcgImgList[6].src,
                    bcgColor: t.data.bcgImgList[6].topColor
                }), t.setNavigationBarColor());
            }
        });
    },
    setNavigationBarColor: function(t) {
        t || this.data.bcgColor;
        wx.setNavigationBarColor({
            frontColor: "#ffffff",
            backgroundColor: this.data.bcgColor
        });
    },
    menuMainMove: function(t) {
        this.data.hasPopped && (this.takeback(), this.setData({
            hasPopped: !1
        }));
        var a = e.windowWidth, o = e.windowHeight, i = t.touches[0], n = i.clientX, s = i.clientY;
        n > a - 40 && (n = a - 40), n <= 90 && (n = 90), s > o - 40 - 60 && (s = o - 40 - 60), 
        s <= 60 && (s = 60);
        var r = {
            left: n,
            top: s
        };
        this.setData({
            pos: r
        });
    },
    getBroadcast: function(t) {
        wx.cloud.callFunction({
            name: "getBroadcast",
            data: {
                hour: new Date().getHours()
            }
        }).then(function(a) {
            var o = a.result.data;
            o && t && t(o[0].message);
        });
    },
    reloadGetBroadcast: function() {
        var t = this;
        this.getBroadcast(function(a) {
            t.setData({
                message: a
            });
        });
    },
    reloadWeather: function() {
        this.data.located ? this.init({}) : (this.search(this.data.searchCity), this.setData({
            searchCity: ""
        }));
    },
    onShow: function() {
        var a = this;
        t.isEmptyObject(this.data.shareInfo) && wx.cloud.callFunction({
            name: "getShareInfo"
        }).then(function(o) {
            var e = o.result;
            e && (t.isEmptyObject(e) || a.setData({
                shareInfo: e
            }));
        });
    },
    onLoad: function() {
        this.reloadPage();
        var t = wx.createInterstitialAd({
            adUnitId: "adunit-7018e65d463eab0a"
        });
        t && t.show().catch(function(t) {
            console.error(t);
        });
    },
    reloadPage: function() {
        this.getCityDatas(), this.reloadInitSetting(), this.reloadWeather(), this.reloadGetBroadcast();
    },
    checkUpdate: function(t) {
        if (t.forceUpdate && wx.getUpdateManager) {
            var a = wx.getUpdateManager();
            a.onCheckForUpdate(function(t) {
                console.error(t);
            }), a.onUpdateReady(function() {
                wx.showModal({
                    title: "更新提示",
                    content: "新版本已下载完成，是否重启应用？",
                    success: function(t) {
                        t.confirm && a.applyUpdate();
                    }
                });
            });
        }
    },
    hideBcgImgArea: function() {
        this.setData({
            bcgImgAreaShow: !1
        });
    },
    initSetting: function(t) {
        var a = this;
        wx.getStorage({
            key: "setting",
            success: function(o) {
                var e = o.data || {};
                a.setData({
                    setting: e
                }), t && t(e);
            },
            fail: function() {
                a.setData({
                    setting: {}
                });
            }
        });
    },
    reloadInitSetting: function() {
        var t = this;
        this.initSetting(function(a) {
            t.checkUpdate(a);
        });
    },
    onShareAppMessage: function(t) {
        var a = this.data.shareInfo, o = Math.floor(8 * Math.random());
        return console.log("dasdasdas" + o), {
            title: a.title || this.data.sharelist[o].name,
            path: a.path || "/pages/index/index",
            imageUrl: this.data.sharelist[o].url
        };
    },
    menuHide: function() {
        this.data.hasPopped && (this.takeback(), this.setData({
            hasPopped: !1
        }));
    },
    menuMain: function() {
        this.data.hasPopped ? (this.takeback(), this.setData({
            hasPopped: !1
        })) : (this.popp(), this.setData({
            hasPopped: !0
        }));
    },
    menuToCitychoose: function() {
        this.menuMain(), wx.navigateTo({
            url: "/pages/citychoose/citychoose"
        });
    },
    menuToSetting: function() {
        this.menuMain(), wx.navigateTo({
            url: "/pages/setting/setting"
        });
    },
    menuToAbout: function() {
        this.menuMain(), wx.navigateTo({
            url: "/pages/about/about"
        });
    },
    menuToShare: function() {
        this.menuMain();
        var t = wx.createRewardedVideoAd({
            adUnitId: "adunit-fa97b8e61d356c04"
        });
        t.load().then(function() {
            return t.show();
        }).catch(function(t) {}), t.onClose(function(a) {
            a && a.isEnded || void 0 === a ? wx.showToast({
                title: "播放完毕",
                duration: 2e3
            }) : wx.showModal({
                title: "提示",
                content: "您提前关闭了广告，无法获得奖励",
                success: function(a) {
                    a.confirm ? console.log("点击了确定") : a.cancel && t.load().then(function() {
                        return t.show();
                    }).catch(function(t) {});
                }
            });
        });
    },
    popp: function() {
        var t = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), a = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), o = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), e = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        });
        t.rotateZ(180).step(), a.translate(-30, -45).rotateZ(180).opacity(1).step(), o.translate(-75, 0).rotateZ(180).opacity(1).step(), 
        e.translate(-30, 45).rotateZ(180).opacity(1).step(), this.setData({
            animationMain: t.export(),
            animationOne: a.export(),
            animationTwo: o.export(),
            animationFour: e.export()
        });
    },
    takeback: function() {
        var t = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), a = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), o = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        }), e = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-out"
        });
        t.rotateZ(0).step(), a.translate(0, 0).rotateZ(0).opacity(0).step(), o.translate(0, 0).rotateZ(0).opacity(0).step(), 
        e.translate(0, 0).rotateZ(0).opacity(0).step(), this.setData({
            animationMain: t.export(),
            animationOne: a.export(),
            animationTwo: o.export(),
            animationFour: e.export()
        });
    }
});